<template>
  <PageLayout class="form-container">
    <a-card>
      <div class="form-title">{{ route.query.id ? "编辑" : "新增" }}代理商</div>
      <div class="form-box w-600">
        <a-form
          :model="formState"
          :rules="rules"
          ref="formRef"
          layout="Vertical"
          :labelCol="{ style: { width: '140px' } }"
        >
          <a-form-item label="代理商类型" name="roleType" required>
            <a-select
              v-model:value="formState.roleType"
              placeholder="请选择代理商类型"
              :options="typeOptions"
            />
          </a-form-item>

          <a-form-item label="开拓设备抽成比例" name="ratio" required>
            <a-input-number
              v-model:value="formState.ratio"
              placeholder="请输入开拓设备抽成比例"
              :precision="2"
              :step="0.01"
              style="width: 100%"
              :max="1.0"
               string-mode
            />
          </a-form-item>

          <a-button
            type="primary"
            class="float-right"
            style="width: 100px"
            @click="save"
            >确定</a-button
          >
        </a-form>
      </div>
    </a-card>
  </PageLayout>
</template>

<script setup>
import {
  postShopUserAdd,
  postAgentVipUpdate,
  postAgentDetail,
} from "@/api/common";
import { useStoreCommonEnum } from "@/store";
import { message } from "ant-design-vue";
import { onMounted } from "vue";
const storeCommonEnum = useStoreCommonEnum();

const route = useRoute();
const router = useRouter();
const formRef = ref(null);

const formState = reactive({
  id: route.query.id,
  roleType: undefined,
  ratio: undefined,
});
const typeOptions = ref([
  { label: "普通代理商", value: 0 },
  { label: "VIP代理商", value: 1 },
]);
const detail = ref({});
onMounted(async () => {
  if (route.query.id) {
    postAgentDetail({
      id: route.query.id,
    }).then((res) => {
      for (let key in formState) {
        formState[key] = res.data[key];
      }
    });
  }
});

const rules = {
  roleType: [{ required: true, message: "请选择代理商类型", trigger: "blur" }],
  ratio: [
    { required: true, message: "请输入开拓设备抽成比例", trigger: "blur" },
  ],
};

const save = () => {
  formRef.value.validate().then(() => {
    postAgentVipUpdate({ ...formState }).then(() => {
      message.success(formState.id ? "编辑成功" : "新增成功");
      router.go(-1);
    });
  });
};
</script>

<style lang="less" scoped>
.btn-box {
  margin-top: 24px;
  margin-left: 130px;
  :deep(.ant-btn) {
    margin-right: 16px;
  }
}
</style>
